﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : Singleton<GameManager>
{
    public AudioSource theMusic;

    public bool startPlaying;

    public BeatScroller theBS;

    public int currentScore;
    public int scorePerNote = 100;
    public int scorePerGoodNote = 125;
    public int scorePerPerfectNote = 150;

    public int currentMultiplier;
    public int multiplierTracker;
    public int[] multiplierThresholds;

    public Text scoreText;
    public Text multiText;

    public int totalNotes;
    public int normalHits;
    public int goodHits;
    public int perfectHits;
    public int missHits;

    public GameObject resultsScreen;

    public Text percentHitText;
    public Text normalsText;
    public Text goodsText;
    public Text perfectsText;
    public Text missesText;
    public Text rankText;
    public Text finalScoreText;

    void Start()
    {
        SetScoreText(0f);
        currentMultiplier = 1;

        totalNotes = FindObjectsOfType<NoteObject>().Length;
    }

    void Update()
    {
        if (!startPlaying)
        {
            if (Input.anyKeyDown)
            {
                startPlaying = true;
                theBS.hasStarted = true;

                theMusic.Play();
            }
        }
        else
        {
            if (!theMusic.isPlaying && !resultsScreen.activeInHierarchy)
            {
                resultsScreen.SetActive(true);

                normalsText.text = "" + normalHits;
                goodsText.text = "" + goodHits;
                perfectsText.text = "" + perfectHits;
                missesText.text = "" + missHits;

                float percentHit = (totalNotes - missHits) / totalNotes * 100f;
                percentHitText.text = percentHit.ToString("F1") + "%";

                string rankVal = "F";

                if(percentHit > 40 && percentHit < 55)
                {
                    rankVal = "D";
                }
                else if(percentHit >= 55 && percentHit < 70)
                {
                    rankVal = "C";
                }
                else if (percentHit >= 70 && percentHit < 85)
                {
                    rankVal = "B";
                }
                else if (percentHit >= 85 && percentHit < 95)
                {
                    rankVal = "A";
                }
                else if (percentHit >= 95)
                {
                    rankVal = "S";
                }
                
                rankText.text = rankVal;
                finalScoreText.text = "" + currentScore;
            }
        }
    }

    public void NoteHit()
    {

        if(currentMultiplier - 1 < multiplierThresholds.Length)
        {
            multiplierTracker++;
            if (multiplierThresholds[currentMultiplier - 1] <= multiplierTracker)
            {
                currentMultiplier++;
            }
        }

        //currentScore += scorePerNote * currentMultiplier;
        SetScoreText(currentScore);
        SetMultiText(currentMultiplier);
    }

    private void SetScoreText(float score)
    {
        scoreText.text = "Score: " + score;
    }
    private void SetMultiText(float multi)
    {
        multiText.text = "Multiplier: x" + multi;
    }

    public void NormalHit()
    {
        currentScore += scorePerNote * currentMultiplier;
        NoteHit();
        normalHits++;
    }
    public void GoodHit()
    {
        currentScore += scorePerGoodNote * currentMultiplier;
        NoteHit();
        goodHits++;
    }
    public void PerfectHit()
    {
        currentScore += scorePerPerfectNote * currentMultiplier;
        NoteHit();
        perfectHits++;
    }

    public void NoteMissed()
    {
        Debug.Log("Missed Note");
        currentMultiplier = 1;
        multiplierTracker = 0;
        SetMultiText(currentMultiplier);
        missHits++;
    }
}
